package com.jf.mall.web;

import com.google.gson.Gson;
import com.jf.mall.domain.User;
import com.jf.mall.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-09-24
 * Time:17:24
 */
@WebServlet(name = "UserServlet", urlPatterns = "/userServlet")
public class UserServlet extends BasicServlet {
    private UserService userService;

    @Override
    public void init() throws ServletException {
        super.init();
        userService = new UserService();
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("user-name");
        String password = req.getParameter("user-password");
        User tempUser = new User(username, password);
        User user = userService.login(tempUser);
        String contextPath = req.getServletContext().getContextPath();
        if (user != null) {
            req.getSession().setAttribute("user", user);
            //req.getRequestDispatcher("/shoppingCartServlet?action=createCart").forward(req, resp);//转发和重定向不能同时使用
            resp.sendRedirect(contextPath + "/views/user/login_ok.jsp");
        } else {
            req.setAttribute("username", username);
            req.setAttribute("warn_login", "用户名或密码错误");
            req.getRequestDispatcher("/views/user/login.jsp").forward(req, resp);
        }
    }

    private void checkExist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        boolean existUser = userService.checkExistUser(username);
        Map<String, Object> json = new HashMap<>();
        json.put("exist", existUser);
        Gson gson = new Gson();
        gson.toJson(json, resp.getWriter());
    }

    private void register(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        HttpSession session = req.getSession();
        String verificationCode = session.getAttribute(KAPTCHA_SESSION_KEY).toString();
        session.removeAttribute(KAPTCHA_SESSION_KEY);
        String code = req.getParameter("code");
        String username = req.getParameter("user-name");
        String password = req.getParameter("user-password");
        String email = req.getParameter("user-email");
        if (verificationCode != null && verificationCode.equals(code)) {
            String contextPath = req.getServletContext().getContextPath();
            if (!userService.checkExistUser(username)) {
                User user = new User(username, password, email);
                boolean register = userService.register(user);
                if (register) {
                    resp.sendRedirect(contextPath + "/views/user/register_ok.jsp");
                } else {
                    resp.sendRedirect(contextPath + "/views/user/register_fail.jsp");
                }
            } else {
                req.setAttribute("active", "register");
                req.setAttribute("warn_register", "用户名已存在");
                req.getRequestDispatcher("/views/user/login.jsp").forward(req, resp);
            }
        } else {
            req.setAttribute("active", "register");
            req.setAttribute("warn_register", "验证码错误");
            req.setAttribute("uname", username);
            req.setAttribute("email", email);
            req.getRequestDispatcher("/views/user/login.jsp").forward(req, resp);
        }
    }
    private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.getSession().removeAttribute("user");
        resp.sendRedirect(req.getContextPath() + "/");
    }
}
